package com.koocloud.electroplatemanage.auth.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.koocloud.electroplatemanage.auth.mapper.IPermissionDao;
import com.koocloud.electroplatemanage.auth.service.IPermissionService;
import com.koocloud.electroplatemanage.pojo.Permission;
import com.koocloud.electroplatemanage.utils.ListUtil;
import org.springframework.stereotype.Service;




@Service
public class IPermissionServiceimpl implements IPermissionService {
	@Resource
	IPermissionDao iPermissionDao;
	/**
	 * 查询权限
	 */
	@Override
	public List<Permission> selectPermission(String id, String permissionName) {
		List<Permission> list =iPermissionDao.selectPermission(id,permissionName);
		for (Permission permission : list) {
            //获取每个页面下的按钮列表
		    List<Permission> selectPermissionByParentId = iPermissionDao.selectPermissionByParentId(String.valueOf(permission.getId()));
		    if (!ListUtil.isEmpty(selectPermissionByParentId)) {
		        //存放每个页面下的所有按钮权限
		        permission.setChildren(selectPermissionByParentId);
            }
        }
		return list;
	}
	/**
	 * 根据id修改权限
	 */
	@Override 
	public Integer updatePermission(Permission permission) {
		Integer num=0;
		if("顶级栏目".equals(permission.getUrl())) {
			//根据id修改权限
			num=iPermissionDao.updatePermission(permission);
		}
		else {
			//根据id修改权限
			num=iPermissionDao.updatePermission(permission);
/*			//根据id获取该页面的所有按钮列表
			List<Permission> selectPermissionByParentId = iPermissionDao.selectPermissionByParentId(String.valueOf(permission.getId()));
			//先删除该页面对应的所有按钮列表
			if (!ListUtil.isEmpty(selectPermissionByParentId)) {
				for (Permission permission2 : selectPermissionByParentId) {
					iPermissionDao.deletePermission(String.valueOf(permission2.getId()));
				}
			}
			//再进行添加该页面对应的按钮列表
			//获取按钮列表
			List<Permission> permissionBtnList = permission.getChildren();
			if (!ListUtil.isEmpty(permissionBtnList)) {
				for (Permission permissionBtn2 : permissionBtnList) {
					permissionBtn2.setParentId(permission.getId());
					//添加该页面的按钮列表
					num = iPermissionDao.addPermission(permissionBtn2);

				}
			}*/
		}

		
		return num;
	}
	/**
	 * 根据id删除权限
	 */
	@Override
	public Integer deletePermission(String id) {
		Integer num= iPermissionDao.deletePermission(id);
		//删除该页面对应下面的所有按钮列表
		//根据id获取该页面的所有按钮列表
        List<Permission> selectPermissionByParentId = iPermissionDao.selectPermissionByParentId(id);
        //先删除该页面对应的所有按钮列表
        if (!ListUtil.isEmpty(selectPermissionByParentId)) {
            for (Permission permission2 : selectPermissionByParentId) {
                iPermissionDao.deletePermission(String.valueOf(permission2.getId()));
            }
        }
		return num;
	}
	/**
	 * 查询权限列表
	 */
	@Override
	public List<Map<String, Object>> selectPermissionList() {
		List<Map<String, Object>> list =iPermissionDao.selectPermissionList();
		return list;
	}
	
	

	/**
	 * 添加权限
	 */
	@Override
	public Integer addPermission(Permission permission) {
	    Integer num=iPermissionDao.addPermission(permission);
	    //获取按钮列表
	    List<Permission> permissionBtnList = permission.getChildren();
	    if (!ListUtil.isEmpty(permissionBtnList)) {
	        for (Permission permissionBtn2 : permissionBtnList) {
	            permissionBtn2.setParentId(permission.getId());
	            //添加该页面的按钮列表
	             num = iPermissionDao.addPermission(permissionBtn2);
	            
	        }
        }	    
		return num;
	}
	
	/**
	 * 查询父级菜单列表
	 */
	@Override
	public List<Map<String, Object>> selectFirstPermissionList() {
		 List<Map<String, Object>> list=iPermissionDao.selectFirstPermissionList();
		return list;
	}
	
	/**
	 * 根据id查看权限
	 */
	@Override
	public Permission   selectPermissionById(String id) {
	    //获取父级id
	    Permission permission =iPermissionDao.selectPermissionById(id);
	    //获取该父级子集的对象
	    List<Permission> chList = iPermissionDao.selectPermissionByParentId(id);
	    //将按钮列表存在子集页面
	    permission.setChildren(chList);
		return permission;
	}
	/**
	 * 根据父级id获取子集的数量
	 */
	@Override
	public String selectChirenNumById(String id) {
		// TODO Auto-generated method stub
		return iPermissionDao.selectChirenNumById(id);
	}
	/**
	 * 查看权限名是否重复
	 */
    @Override
    public Integer selectPermissionByName(String permissionName) {
        // TODO Auto-generated method stub
        return iPermissionDao.selectPermissionByName(permissionName);
    }

}
